/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
// copy from vue-loader
import "@rspack/core/module";

import noTry = require("no-try");

// vue hmr
type HMRRuntime = {
  createRecord: Function;
  rerender: Function;
  reload: Function;
};

function genHotReloadCode(
  id: string,
  templateRequest: string | undefined,
): string {
  return /* js */ `
/* hot reload */
if (__webpack_module__.hot) 
{
  __exports__.__hmrId = "${id}";
  const api = __VUE_HMR_RUNTIME__;
  __webpack_module__.hot.accept();
  if (!api.createRecord('${id}', __exports__)) {
    api.reload('${id}', __exports__)
  }
  ${templateRequest ? genTemplateHotReloadCode(id, templateRequest) : ""}
}
`;
}
function genTemplateHotReloadCode(id: string, request: string) {
  return /* js */ `
__webpack_module__.hot.accept(${request}, () => 
{
  api.rerender('${id}', render)
})
`;
}

// to split
const ExportHelper = (sfc: any, props: [string, string][]) => {
  const target = sfc.__vccOpts || sfc;
  for (const [key, val] of props) {
    target[key] = val;
  }
  return target;
};

export { ExportHelper, genHotReloadCode };
